#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iterator>
#include <QFileDialog>
#include <QFuture>
#include <QtConcurrent>
#include <QDebug>
#define _DLLAPI 1
#include "Windows.h"
#include "ocr.h"
#include <QTextCodec>
#include <QByteArray>
#include <iostream>
#include <vector>
#include <QDir>
#include <QStringList>
QByteArray AnsiToUtf8(QByteArray &ansi)
{
#ifdef _CUR_SYS_LINUX_
    QByteArray utf8;
    char *buf = new char[ansi.size() * 2];
    char utf8Buf[] = { "utf-8" };
    char gb2312Buf[] = { "gb2312" };
    //ansi.resize(ansi.size()*2);
    int code = code_convert(gb2312Buf, utf8Buf, ansi.data(), ansi.size(), buf, 2 * ansi.size());
    if (code == -1) {
        delete[]buf;
        utf8 = "";
        return utf8;
    }
    utf8 = buf;
    delete[]buf;
    return utf8;
#else
    int len;
    QByteArray result;
    //ANSI转UNICODE
    len = MultiByteToWideChar(CP_ACP, NULL, ansi.data(), -1, NULL, 0);
    WCHAR * unicode = new WCHAR[len + 1];
    memset(unicode, 0, len * 2 + 2);
    MultiByteToWideChar(CP_ACP, NULL, ansi.data(), -1, unicode, len);
    //UNICODE转utf8
    len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
    char *utf8 = new char[len + 1];
    memset(utf8, 0, len + 1);
    WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len, NULL, NULL);
    //赋值
    result = utf8;
    delete[] unicode;
    delete[] utf8;
    return result;
#endif
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_file_btn_clicked()
{
    isFile = 1;
    QString testFileName = QFileDialog::getOpenFileName(this,tr(""),"",tr("Img(*.jpg);PNG(*.png)"));
    ui->file_edit->setText(testFileName);
}

void MainWindow::on_get_btn_clicked()
{
    ui->get_btn->setText("提取ing...");
    ui->get_btn->setDisabled(1);
    QFuture< QString> future;
    if(isFile==1)
        future= QtConcurrent::run(this, &MainWindow::getText,ui->file_edit->text());
    else if(isFile==0)
        future= QtConcurrent::run(this, &MainWindow::getDirText,ui->dir_edit->text());
    else if(isFile==2)
        future= QtConcurrent::run(this, &MainWindow::getWebText,ui->file_edit_2->text());
    else if(isFile==3)
        future= QtConcurrent::run(this, &MainWindow::getVoiceText,ui->voice_edit->text());
    while(!future.isFinished())
    {
        QApplication::processEvents(QEventLoop::AllEvents, 100);
    }
    ui->textEdit->setText(future);
    ui->get_btn->setText("提取");
    ui->get_btn->setEnabled(1);
}

void MainWindow::on_dir_btn_clicked()
{
    isFile = 0;
    QString srcDirPath = QFileDialog::getExistingDirectory(
                this, "choose src Directory",
                "E:\\");

    if (srcDirPath.isEmpty())
    {
        return;
    }
    else
    {
        qDebug() << "srcDirPath=" << srcDirPath;
        srcDirPath += "/";
    }
    ui->dir_edit->setText(srcDirPath);
}

QString MainWindow::getText(const QString & path)
{
   ocr ooo;
   qDebug()<<"path"<<path<<"\n  data = "<<path.toUtf8().data();

   string str =ooo.baiDuOcr(path.toLocal8Bit().data());
   QByteArray arr = QByteArray(str.c_str());
   std::cout<<"string = "<<str<<endl;
   QString utf8_str = QString(AnsiToUtf8(arr));
   qDebug()<<utf8_str;
   return utf8_str ;

}

QString MainWindow::getDirText(const QString & dir)
{
    QStringList filelist = getFileNames(dir);
    QString ret;
    for(auto it :filelist){
        ret += QString("文件名：%1 \n").arg(dir+it);
        ret += getText(dir+it);
        ret += "===============================================\n";

    }
    return ret;
}

QString MainWindow::getWebText(const QString & path)
{
    ocr ooo;
    qDebug()<<"path"<<path<<"\n  data = "<<path.toUtf8().data();

    string str =ooo.baiduGetWebImage(path.toLocal8Bit().data());
    QByteArray arr = QByteArray(str.c_str());
    std::cout<<"string = "<<str<<endl;
    QString utf8_str = QString(AnsiToUtf8(arr));
    qDebug()<<utf8_str;
    return utf8_str ;
}

QString MainWindow::getVoiceText(const QString & path)
{
    QStringList list ;
    list = path.split(".");
    ocr ooo;
    qDebug()<<list.at(list.size()-1)<<path;
    string str =ooo.baiDuvoiceOcr(path.toLocal8Bit().data(),list.at(list.size()-1).toLocal8Bit().data());
    QByteArray arr = QByteArray(str.c_str());
    std::cout<<"string = "<<str<<endl;
    QString utf8_str = QString(AnsiToUtf8(arr));

    return utf8_str ;
}

QStringList MainWindow::getFileNames(const QString& path)
{


    QDir dir(path);
    QStringList nameFilters;
    nameFilters << "*.jpg" << "*.png";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    return files;

}



void MainWindow::on_file_edit_2_textChanged(const QString &arg1)
{
    isFile = 2;
}

void MainWindow::on_voice_btn_clicked()
{
    isFile = 3;
    QString testFileName = QFileDialog::getOpenFileName(this,tr(""),"",tr("PCM(*.pcm)"));

    ui->voice_edit->setText(testFileName);
}
